#include "settingdialog.h"
#include "ui_settingdialog.h"
#include <QColorDialog>
#include <QDialogButtonBox>
#include <QPalette>
#include <QPushButton>
#include <QSettings>

SettingDialog::SettingDialog(IWingPlugin *plugin) {
  // 这里自己确保 plugin != nullptr

  dialog = plugin->newDDialog();
  dialog->resize(450, 500);
  dialog->setWindowTitle(tr("SettingDialog"));

  plugin->addSpace(dialog, 20);
  auto btn_elf_header = new QPushButton(tr("elf_header color"), dialog);
  btn_elf_header->setMinimumWidth(400);
  connect(btn_elf_header, &QPushButton::clicked, this,
          &SettingDialog::on_btn_elf_header_clicked);
  plugin->addContent(dialog, btn_elf_header, Qt::AlignCenter);
  plugin->addSpace(dialog, 20);
  auto btn_program_table = new QPushButton(tr("program_table color"), dialog);
  btn_program_table->setMinimumWidth(400);
  connect(btn_program_table, &QPushButton::clicked, this,
          &SettingDialog::on_btn_program_table_clicked);
  plugin->addContent(dialog, btn_program_table, Qt::AlignCenter);
  plugin->addSpace(dialog, 20);
  auto btn_section_header =
      new QPushButton(tr("section_header_table color"), dialog);
  btn_section_header->setMinimumWidth(400);
  connect(btn_section_header, &QPushButton::clicked, this,
          &SettingDialog::on_btn_section_header_clicked);
  plugin->addContent(dialog, btn_section_header, Qt::AlignCenter);
  plugin->addSpace(dialog, 20);
  auto btn_symbol = new QPushButton(tr("symbol_table color"), dialog);
  btn_symbol->setMinimumWidth(400);
  connect(btn_symbol, &QPushButton::clicked, this,
          &SettingDialog::on_btn_symbol_clicked);
  plugin->addContent(dialog, btn_symbol, Qt::AlignCenter);
  plugin->addSpace(dialog, 20);
  auto btn_dysymbol = new QPushButton(tr("dynamic_symbol_table color"), dialog);
  btn_dysymbol->setMinimumWidth(400);
  connect(btn_dysymbol, &QPushButton::clicked, this,
          &SettingDialog::on_btn_dysymbol_clicked);
  plugin->addContent(dialog, btn_dysymbol, Qt::AlignCenter);
  plugin->addSpace(dialog, 25);
  auto dbox = new QDialogButtonBox(
      QDialogButtonBox::Cancel | QDialogButtonBox::Ok, dialog);
  plugin->addContent(dialog, dbox);
  connect(dbox, &QDialogButtonBox::accepted, this,
          &SettingDialog::on_buttonBox_accepted);
  connect(dbox, &QDialogButtonBox::rejected, dialog, &QDialog::reject);

  settings = Settings::instance();
  settings->getAllColor(elf_header_color, program_table_color,
                        section_header_color, symbol_color, dysymbol_color);

  QPalette pe;
  pe.setColor(QPalette::ButtonText, elf_header_color);
  btn_elf_header->setPalette(pe);
  pe.setColor(QPalette::ButtonText, program_table_color);
  btn_program_table->setPalette(pe);
  pe.setColor(QPalette::ButtonText, section_header_color);
  btn_section_header->setPalette(pe);
  pe.setColor(QPalette::ButtonText, symbol_color);
  btn_symbol->setPalette(pe);
  pe.setColor(QPalette::ButtonText, dysymbol_color);
  btn_dysymbol->setPalette(pe);
}

SettingDialog::~SettingDialog() { delete dialog; }

int SettingDialog::show() { return dialog->exec(); }

#define CHOOSECOLOR(color)                                                     \
  if (chooseColor(color, color)) {                                             \
    settings->saveColor(SettingColor::color, color);                           \
  }

void SettingDialog::on_btn_elf_header_clicked() {
  CHOOSECOLOR(elf_header_color)
}

void SettingDialog::on_btn_program_table_clicked() {
  CHOOSECOLOR(program_table_color)
}

void SettingDialog::on_btn_section_header_clicked() {
  CHOOSECOLOR(section_header_color)
}

void SettingDialog::on_btn_symbol_clicked() { CHOOSECOLOR(symbol_color) }

void SettingDialog::on_btn_dysymbol_clicked() { CHOOSECOLOR(dysymbol_color) }

void SettingDialog::on_buttonBox_accepted() {
  settings->saveAllColor(elf_header_color, program_table_color,
                         section_header_color, symbol_color, dysymbol_color);
  dialog->done(1);
}

bool SettingDialog::chooseColor(QColor orign, QColor &color) {
  QColorDialog d;
  d.setCurrentColor(orign);
  if (d.exec()) {
    color = d.currentColor();
    return true;
  }
  return false;
}
